package com.roncoo.example.controller.valid;

import com.roncoo.example.valid.First;
import com.roncoo.example.vo.People;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class FirstController {

  @RequestMapping("/addPeople")
  //不需验证ID
  public @ResponseBody
  String addPeople(@Validated({First.class}) People p, BindingResult result) {
    System.out.println("people's ID:" + p.getId());
    if (result.hasErrors()) {
      return "0" + result.getAllErrors().get(0);
    }
    return "1";
  }

  @RequestMapping("/updatePeople")
  //需要验证ID
  public @ResponseBody
  String updatePeople(@Validated({First.class}) People p, BindingResult result) {
    System.out.println("people's ID:" + p.getId());
    if (result.hasErrors()) {
      return "0";
    }
    return "1";
  }


}
